// pages/index3/index3.js
var common = require("../../utils/Conmon.js");
var wxParse = require('../../wxParse/wxParse.js');
var appData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    viewmodel: true,
    ruesmodel: true,
    OrderMoney: 0.00,
    items: [],
    PayType: -1,
    orderId: ""
  },
  // radio  
  radioChange: function(e) {


  },
  changePaytype: function(e) {
    this.setData({
      PayType: e.currentTarget.id,
    });
    this.gopay();
  },
  viewModel: function() {
    this.setData({
      viewmodel: !(this.data.viewmodel),
    })
  },
  // 协议
  ruesModel: function() {

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    common.pageInit(function() {});
    that.setData({
      orderId: options.OrderId
    });

    that.LoadData();
    that.loadAgreement();
    that.LoadpayType();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  LoadData: function() {

    var that = this;
    wx.request({
      url: appData.apiurl + "/mobile/api/Unionstore/GetApply",
      data: {
        "custID": appData.custID,
        "userID": wx.getStorageSync('UserInfo').UserID
      },
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'X_ManuName': appData.manuName
      },
      success: function(res) {
        var m = res.data.content;
        if (m != null) {
          that.setData({
            OrderMoney: m.OrderMoney
          });
        }


      },
      fail: function() {

        wx.showToast({
          title: '检查权限失败',
          duration: 2000
        })
      },
      complete: function() {

      }
    })
  },
  loadAgreement: function() {
    var that = this;
    wx.request({
      url: appData.apiurl + "/mobile/api/Unionstore/GetAgreement",
      data: {
        "custID": appData.custID
      },
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'X_ManuName': appData.manuName
      },
      success: function(res) {
        var data = res.data.content;
        if (data.Agreement) {
          wxParse.wxParse('Agreement', 'html', data.Agreement, that, 5)
        }


      },
      fail: function() {

        wx.showToast({
          title: '获取协议失败',
          duration: 2000
        })
      },
      complete: function() {

      }
    })

  },
  LoadpayType: function() {
    var that = this;
    wx.request({
      url: appData.apiurl + "/mobile/api/OrderForm/GetPayTypeListByTerminal",
      data: {
        "custId": appData.custID,
        "useTerminal": 3
      },
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'X_ManuName': appData.manuName
      },
      success: function(res) {
        var data = res.data.content;
        that.setData({
          items: data

        });


      },
      fail: function() {

        wx.showToast({
          title: '获取协议失败',
          duration: 2000
        })
      },
      complete: function() {

      }
    })

  },
  //去付款
  gopay: function(e) {
    var that = this;

    var payType = that.data.PayType;
    if (payType == -1) {
      wx.showModal({
        content: '请选择支付方式',
        showCancel: false,
        confirmText: "确定"
      })

      return;
    }
    var openid = wx.getStorageSync('openId');

    wx.request({
      url: appData.apiurl + '/mobile/api/OrderForm/GoPay3',
      data: {
        CustId: appData.custID,
        OrderId: that.data.orderId,
        OpenId: openid,
        PayTypeId: that.data.PayType
      },
      method: 'GET',
      header: {
        'X_ManuName': appData.manuName
      },
      success: function(res) {

        if (res.data.code == 1200) {
          var jsonStr = JSON.parse(res.data.content);
          wx.requestPayment({
            'timeStamp': jsonStr.timeStamp,
            'nonceStr': jsonStr.nonceStr,
            'package': jsonStr.package,
            'signType': jsonStr.signType,
            'paySign': jsonStr.paySign,
            'success': function(res) {

              wx.switchTab({
                url: '/pages/my/my'
              })
            },
            'fail': function(res) {


            },
            'complete': function(res) {


            }
          });
        } else {

          wx.showModal({
            content: res.data.msg,
            showCancel: false,
            confirmText: "确定"
          })

        }
      },
      fail: function(res) {

      }
    })

  }
})